﻿<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            {{~ for column in query_columns ~}}
            <el-form-item label="{{ column.label }}" prop="{{ column.prop }}">
                {{~ if column.data_type == "string" ~}}
                <el-input v-model="queryParams.{{ column.prop }}" placeholder="请输入{{ column.label }}" clearable style="width: 240px" />
                {{~ else if column.display_type == "radio" || column.display_type == "selectRadio" || column.display_type == "select" || column.display_type == "selectMulti" ~}}
                <el-select v-model="queryParams.{{ column.prop }}" placeholder="请选择{{column.label}}" clearable style="width: 240px">
                    <el-option v-for="dict in {{column.dict_type}}" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
                {{~ else if column.data_type == "DateTime" ~}}
                <el-date-picker v-model="dateRange{{ column.field_name }}" type="datetimerange" range-separator="-" start-placeholder="开始日期"
                                end-placeholder="结束日期" placeholder="请选择{{ column.label }}" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime" clearable>
                </el-date-picker>
                {{~ end ~}}
            </el-form-item>
            {{~ end ~}}
            <el-form-item>
                <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
                <el-button icon="refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            {{~ for f in func ~}}
            {{~ if f == "add"~}}
            <el-col :span="1.5">
                <el-button type="primary" plain icon="plus" @click="handleAdd">新增</el-button>
            </el-col>
            {{~else if f == "update"~}}
            <el-col :span="1.5">
                <el-button type="success" plain icon="edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            {{~else if f == "del"~}}
            <el-col :span="1.5">
                <el-button type="danger" plain icon="delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            {{~else if f == "export"~}}
            <el-col :span="1.5">
                <el-button type="warning" plain icon="download" @click="handleExport">导出</el-button>
            </el-col>
            {{~end~}}
            {{~end~}}
            <right-toolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            {{~ for column in columns ~}}
            {{~ if column.is_visible == true~}}
            <el-table-column label="{{ column.label }}" align="center" prop="{{ column.prop }}" :show-overflow-tooltip="true" v-if="columns.showColumn('{{ column.prop }}')">
                <template #default="scope">
                    {{~ if column.display_type == "radio" || column.display_type == "selectRadio" || column.display_type == "select" || column.display_type == "selectMulti" ~}}
                    <dict-tag :options="{{ column.dict_type }}" :value="scope.row.{{ column.prop }}" />
                    {{~ else if column.display_type == "datetime" ~}}
                    {{ "{{ parseTime(scope.row." + column.prop + ") }}" }}
                    {{~ else ~}}
                    {{ "{{ scope.row." + column.prop + "}}" }}
                    {{~ end ~}}
                </template>
            </el-table-column>
            {{~ end ~}}
            {{~ end ~}}

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    {{~ for f in func ~}}
                    {{~ if f == "update"~}}
                    <el-button type="primary" text icon="edit" @click="handleUpdate(scope.row)">修改</el-button>
                    {{~else if f == "del"~}}
                    <el-button type="primary" text icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                    {{~else if f == "view"~}}
                    <el-button type="primary" text icon="view" @click="handleView(scope.row)">查看</el-button>
                    {{~end~}}
                    {{~end~}}
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageIndex" v-model:limit="queryParams.pageSize"
                    @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body draggable align-center>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="20">
                    {{~ for column in edit_columns ~}}
                    {{~if column.is_editable == true~}}
                    {{~if column.display_type == "input"~}}
                    <el-col :lg="24">
                        <el-form-item label="{{column.label}}" prop="{{ column.prop}}">
                            {{~if column.data_type == "DataTime"~}}
                            <el-date-picker v-model="form.time" type="datetime" />
                            {{~else~}}
                            <el-input v-model="form.{{column.prop}}" placeholder="请输入{{column.label}}" clearable />
                            {{~end~}}
                        </el-form-item>
                    </el-col>
                    {{~ else if column.display_type == "radio" || column.display_type == "selectRadio" || column.display_type == "select" || column.display_type == "selectMulti" ~}}
                    <el-col :lg="24">
                        <el-form-item label="{{column.label}}" prop="{{ column.prop}}">
                            <el-select v-model="form.{{ column.prop }}" placeholder="请选择{{column.label}}" clearable>
                                <el-option v-for="dict in {{column.dict_type}}" :key="dict.value" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    {{~else~}}
                    {{~end~}}
                    {{~end~}}
                    {{~end~}}
                </el-row>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="submitForm" v-if="operateType==0||operateType==1">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { list{{ entity_name }}, get{{ entity_name }}, del{{ entity_name }}, add{{ entity_name }}, update{{ entity_name }} } from "{{ api_path }}";

const { proxy } = getCurrentInstance()

{{~ if dictionary_columns.size > 0 ~}}
//字典数据
const dicts = [{{~ for column in dictionary_columns ~}}"{{ column.dict_type }}" {{~ if !for.last ~}}, {{~ end ~}} {{~ end ~}}];
const dictRefs = proxy.useDict(...dicts);
const { {{~ for column in dictionary_columns ~}} {{ column.dict_type }} {{~ if !for.last ~}}, {{~ end ~}} {{~ end ~}} } = dictRefs;
{{~end~}}

//弹窗操作类型
//0:新增，1：修改，2:查看
const operateType = ref(0)
//表单ref
const formRef = ref();
// 遮罩层
const loading = ref(true);
// 选中数组
const selected = ref([]);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 数据集
const dataList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

//默认时间范围
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])
{{~ for column in query_columns ~}}
{{ if column.data_type == "DateTime" }}
//定义{{ column.label }}查询参数
const dateRange{{ column.field_name }} = ref([]);
{{ end }}
{{~ end ~}}

// 查询参数
const queryParams = reactive({
  pageIndex: 1,
  pageSize: 10,
{{~ for column in query_columns ~}}
    {{~ if column.data_type != "DateTime" ~}}
  {{ column.prop }}: undefined,
    {{~ end ~}}
{{~ end ~}}
});



const state = reactive({
  single: true,
  multiple: true,
  form: {

  },
  rules: {
    //customerId: [{ required: true, message: "客户id不能为空", trigger: "blur"    , type: "number"  }],
    //name: [{ required: true, message: "商品名称不能为空", trigger: "blur"     }],
  },
  options: {
    // 商品种类 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
     typeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)


//所有列
const columns = ref([
{{~ for column in columns ~}}
{{~ if column.is_visible == true ~}}
    { visible: {{ column.is_visible }}, prop: '{{ column.prop }}', label: '{{ column.label }}' }{{ if for.last == false }},{{ end }}
{{~ end ~}}
{{~ end ~}}
]);

// 获取列表方法
const getList = () => {
  loading.value = true;
{{~ for column in query_columns ~}}
{{~ if column.data_type == "DateTime" ~}}
//添加时间范围参数
  proxy.addDateRange(queryParams, dateRange{{ column.field_name }}.value, '{{ column.field_name }}');
{{~ end ~}}
{{~ end ~}}
  list{{ entity_name }}(queryParams).then((response) => {
    dataList.value = response.result;
    total.value = response.totalNum;
    loading.value = false;
  });
};

// 重置表单
const reset = () => {

};

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageIndex = 1;
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  reset();
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  selected.value = selection.map((item) => item.{{primary_key}});
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  operateType.value = 0;
  open.value = true;
  title.value = "添加数据";
};

// 修改按钮操作
const handleUpdate = (row) => {
  reset();
  operateType.value = 1;
  const key = row.{{primary_key}} || selected.value;
  get{{ entity_name }}(key).then((response) => {
    Object.assign(form.value, response);
    open.value = true;
    title.value = "修改数据";
  });
};


//查看
const handleView = (row) => {
  reset();
  operateType.value = 2;
  var key = row.{{primary_key}};
  get{{ entity_name }}(key).then((response) => {
     Object.assign(form.value, response);
     open.value = true;
     title.value = "查看明细";
  });
}

// 提交按钮
const submitForm = () => {
  const validateForm = formRef.value?.validate;
  if (validateForm) {
    validateForm((valid) => {
      if (valid) {
        const apiCall = form.value.{{primary_key}} ? update{{ entity_name }} : add{{ entity_name }};
        apiCall(form.value).then(() => {
          proxy.$modal.msgSuccess("操作成功");
          open.value = false;
          getList();
        });
      }
    });
  }
};

//取消弹窗
const cancel = () => {
  open.value=false;
  proxy.resetForm("form")
}

// 删除按钮操作
const handleDelete = (row) => {
  const keys = [].concat(row.{{primary_key}} || selected.value);
  proxy.$modal.confirm("是否确认删除编号为" + keys + "的数据项？")
    .then(() => del{{ entity_name }}(keys))
    .then(() => {
      getList();
       proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

// 导出按钮操作
const handleExport = () => {
  download("system/{{ base }}/export", queryParams, `menu_${new Date().getTime()}.xlsx`);
};

// 页面加载时调用获取列表方法
onMounted(getList);
</script>
